//
//  OrderListCell.m
//  danpin
//
//  Created by chuangjia on 27/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderListCell.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"
@interface OrderListCell ()
{
    
}
@property(nonatomic,strong)UILabel *sectionLabel;
@property(nonatomic,strong)UILabel *statusKey;
@property(nonatomic,strong)UIView *lineview;
//
@property(nonatomic, strong) TTTAttributedLabel* title;
@property(nonatomic,strong) UIImageView *titleIcon;
@property(nonatomic,strong)UIView *line2view;
//
@property(nonatomic,strong)UILabel *ddyfkey;
@property(nonatomic,strong)UIButton *statuBtn;

@end
@implementation OrderListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _sectionLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _sectionLabel.backgroundColor = [UIColor clearColor];
        _sectionLabel.textColor = CONTEXT_COLOR;
        _sectionLabel.textAlignment = NSTextAlignmentLeft;
        _sectionLabel.font = FONT(12);
        [self.contentView addSubview:_sectionLabel];
        
        _statusKey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _statusKey.backgroundColor = [UIColor clearColor];
        _statusKey.textColor = [UIColor orangeColor];
        _statusKey.textAlignment = NSTextAlignmentRight;
        _statusKey.font = FONT(14);
        [self.contentView addSubview:_statusKey];
        
        _lineview =[[UIView alloc] initWithFrame:CGRectZero];
        _lineview.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineview];
        
        //
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*8,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH* 10,40)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkTextColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _title.numberOfLines=0;
        _title.font = FONT(13);
        [self.contentView addSubview:_title];
        
        _titleIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
        _titleIcon.backgroundColor=[UIColor clearColor];
        _titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleIcon];
    
        
        _line2view =[[UIView alloc] initWithFrame:CGRectZero];
        _line2view.backgroundColor=UIColorFromRGB(0xcccccc);
        [self.contentView addSubview:_line2view];
        //
        _ddyfkey =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _ddyfkey.backgroundColor = [UIColor clearColor];
        _ddyfkey.textColor = TITLE_COLOR;
        _ddyfkey.textAlignment = NSTextAlignmentLeft;
        _ddyfkey.font = FONT(14);
        [self.contentView addSubview:_ddyfkey];
        
        //
        _statuBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _statuBtn.backgroundColor = [UIColor clearColor];
        _statuBtn.layer.borderColor = [UIColor orangeColor].CGColor;
        _statuBtn.layer.borderWidth =1;
        _statuBtn.titleLabel.font = FONT(12);
        [_statuBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_statuBtn addTarget:self action:@selector(clickHelpEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_statuBtn];
    
//        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    
    self.data =model;
    //
    NSDictionary *goods_info = [dict dictionaryObjectForKey:@"goods_info"];
    if (goods_info && [goods_info isKindOfClass:[NSDictionary class]]) {
        //
        _sectionLabel.text =[goods_info stringForKey:@"goods_from" withDefaultValue:@""];
        _sectionLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 0,DEVICE_WIDTH/2, 40);
        
        
        NSString *status_zh =[dict stringForKey:@"status_zh" withDefaultValue:@""];
        _statusKey.text = [NSString stringWithFormat:@"%@",status_zh];
        _statusKey.frame =CGRectMake(DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH,(40-16)/2, DEVICE_WIDTH/2, 16);
        _lineview.frame =CGRectMake(0, 39.5,DEVICE_WIDTH, 0.5);
        //
        
        NSString *urlstr =[goods_info stringForKey:@"good_img" withDefaultValue:@""];
        if (urlstr.length>0) {
            _titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*7, PHOTO_FRAME_WIDTH*7);
            [_titleIcon sd_setImageWithURL:[NSURL URLWithString:urlstr]];
        }
        float fheight = 0;
        NSString *title =[goods_info stringForKey:@"good_name" withDefaultValue:@""];
        if (title.length>0) {
            [self.title setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
             {
                 [attributedString addAttribute:NSFontAttributeName
                                          value:FONT(12)
                                          range:NSMakeRange(0,[attributedString length])];

                 //设置字体颜色
                 [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
                 
                 CGFloat lineSpace = 1.0f;
                 CTParagraphStyleSetting lineSpaceStyle;
                 lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
                 lineSpaceStyle.valueSize = sizeof(lineSpace);
                 lineSpaceStyle.value =&lineSpace;
                 
                 //设置文本段间距
                 CGFloat paragraphSpacing = 0.0f;
                 CTParagraphStyleSetting paragraphSpaceStyle;
                 paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
                 paragraphSpaceStyle.valueSize = sizeof(CGFloat);
                 paragraphSpaceStyle.value = &paragraphSpacing;
                 
                 //创建设置数组
                 CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
                 CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
                 
                 //给文本添加设置
                 [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
                 return attributedString;
                 
             }];
            
            fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*10) str:self.title.attributedText];
            
            if (fheight>40) {
                fheight=50;
            }
            
            self.title.frame = CGRectMake(PHOTO_FRAME_WIDTH*9, PHOTO_FRAME_WIDTH*5,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*11, fheight);
        }
        
    }
    
    _line2view.frame =CGRectMake(0,  129.5,DEVICE_WIDTH, 0.5);
    
    //
    
    NSString *total_fee =[dict stringForKey:@"total_fee" withDefaultValue:@""];
    NSString *fee_type =[dict stringForKey:@"fee_type" withDefaultValue:@""];
    _ddyfkey.text = [NSString stringWithFormat:@"订单金额: %@%@",fee_type,total_fee];
    CGSize size = [_ddyfkey boundingRectWithSize:CGSizeMake(DEVICE_WIDTH/2, 20)];
    _ddyfkey.frame =CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*14,size.width, 20);
    _statuBtn.frame =CGRectMake(DEVICE_WIDTH - PHOTO_FRAME_WIDTH - 70,PHOTO_FRAME_WIDTH*14, 70, 20);
    NSString *status =[dict stringForKey:@"status" withDefaultValue:@""];
    _statuBtn.hidden=NO;
    if ([status intValue]==0) {
        [_statuBtn setTitle:@"去支付" forState:UIControlStateNormal];
    }else if ([status intValue]==4 || [status intValue]==6) {
        NSDictionary *dictt = [dict dictionaryObjectForKey:@"shop_order"];
        NSDictionary* order_tariff =[dictt dictionaryObjectForKey:@"order_tariff"];
        if(order_tariff && [order_tariff isKindOfClass:[NSDictionary class]])
        {
            int statuss = [[order_tariff numberStringForKey:@"status"] intValue];
            if (statuss==0) {
                if ([status intValue]==4) {
                     [_statuBtn setTitle:@"补运费" forState:UIControlStateNormal];
                }else if ([status intValue]==6)
                {
                    [_statuBtn setTitle:@"补关税" forState:UIControlStateNormal];
                }
               
            }else
            {
                _statuBtn.frame =CGRectZero;
                _statuBtn.hidden=YES;
            }
        }else
        {
            _statuBtn.frame =CGRectZero;
            _statuBtn.hidden=YES;
        }
      
    }else if ([status intValue]==8) {
        [_statuBtn setTitle:@"确认收货" forState:UIControlStateNormal];
    }else if ([status intValue]==9) {
        [_statuBtn setTitle:@"点评晒单" forState:UIControlStateNormal];
    }else
    {
        _statuBtn.frame =CGRectZero;
        _statuBtn.hidden=YES;
    }
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickHelpEvent
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_help,nil,self.data,nil,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return PHOTO_FRAME_WIDTH*17;
}


@end
